package com.imax.center.controller;

import com.imax.center.annotation.NoAuth;
import com.imax.center.model.*;
import com.imax.center.service.EzvizTokenService;
import com.imax.center.common.Result;
import com.imax.center.service.DeviceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 萤石云相关接口控制器
 */
@RestController
@RequestMapping("/api/ezviz")
@Slf4j
@Api(tags = "萤石云接口")
public class EzvizController {

    @Autowired
    private EzvizTokenService ezvizTokenService;

    @Autowired
    private DeviceService deviceService;

    @ApiOperation(value = "获取萤石云访问令牌", notes = "获取萤石云开放平台的accessToken")
    @GetMapping("/token")
    @NoAuth
    public Result<String> getAccessToken() {
        try {
            String token = ezvizTokenService.getAccessToken();
            return Result.success(token, "获取访问令牌成功");
        } catch (Exception e) {
            log.error("获取萤石云token异常: ", e);
            return Result.error("系统异常");
        }
    }

    @ApiOperation(value = "获取设备列表", notes = "获取萤石云平台的设备列表")
    @PostMapping("/device/list")
    public Result<List<EzvizDevice>> getDeviceList() {
        return Result.success(deviceService.getAllDeviceList());
    }

    @ApiOperation(value = "获取通道列表", notes = "获取萤石云平台的通道列表")
    @PostMapping("/channel/list")
    public Result<List<EzvizChannel>> getChannelList() {
        return Result.success(deviceService.getAllChannelList());
    }

    /**
     * 获取设备的预览或回放URL
     * 该接口用于获取指定设备的实时预览或录像回放的URL地址。
     * @param request 包含设备序列号、通道号、地址类型、开始时间、结束时间和回放速度的请求对象
     * @return 包含设备URL信息的结果对象
     */
    @ApiOperation(value = "获取设备URL", notes = "获取萤石云设备的预览或回放URL")
    @PostMapping("/device/url")
    public Result<EzvizDeviceUrl> getDeviceUrl(@RequestBody EzvizDeviceUrlRequest request) {
        
        log.info("获取设备URL请求，设备序列号: {}, 通道号: {}, 类型: {}, 开始时间: {}, 结束时间: {}, 回放速度: {}", 
                request.getDeviceSerial(), request.getChannelNo(), request.getType(), request.getStartTime(), request.getStopTime(), request.getPlaybackSpeed());
        
        try {
            EzvizDeviceUrl deviceUrl = deviceService.getDeviceUrl(
                    request.getDeviceSerial(), request.getChannelNo(), request.getType(), request.getStartTime(), request.getStopTime(), request.getPlaybackSpeed());
            
            return Result.success(deviceUrl, "获取设备URL成功");
        } catch (Exception e) {
            log.error("获取设备URL异常: ", e);
            return Result.error("获取设备URL失败: " + e.getMessage());
        }
    }
    
    /**
     * 批量获取设备的预览或回放URL
     * 该接口用于批量获取多个设备的实时预览或录像回放的URL地址。
     * @param request 包含多个设备的序列号、通道号以及共同的地址类型、开始时间、结束时间和回放速度的请求对象
     * @return 包含多个设备URL信息的结果对象
     */
    @ApiOperation(value = "批量获取设备URL", notes = "批量获取萤石云设备的预览或回放URL")
    @PostMapping("/device/url/batch")
    public Result<EzvizBatchDeviceUrlResponse> getBatchDeviceUrls(@RequestBody EzvizBatchDeviceUrlRequest request) {
        log.info("批量获取设备URL请求: {}", request);
        
        try {
            EzvizBatchDeviceUrlResponse response = deviceService.getBatchDeviceUrls(request);
            return Result.success(response, "批量获取设备URL成功");
        } catch (Exception e) {
            log.error("批量获取设备URL异常: ", e);
            return Result.error("批量获取设备URL失败: " + e.getMessage());
        }
    }
    
    /**
     * 萤石云设备抓拍图片
     * 该接口用于获取指定设备的实时抓拍图片URL。
     * @param deviceSerial 设备序列号
     * @param channelNo 通道号，默认为1
     * @return 包含设备抓拍图片URL的结果对象
     */
    @ApiOperation(value = "设备抓拍图片", notes = "获取萤石云设备的实时抓拍图片URL")
    @GetMapping("/device/capture")
    public Result<String> captureDeviceImage(
            @RequestParam String deviceSerial,
            @RequestParam(required = false) Integer channelNo) {
        
        log.info("设备抓拍请求，设备序列号: {}, 通道号: {}", deviceSerial, channelNo);
        
        try {
            String picUrl = deviceService.captureDeviceImage(deviceSerial, channelNo);
            return Result.success(picUrl, "设备抓拍成功");
        } catch (Exception e) {
            log.error("设备抓拍异常: ", e);
            return Result.error("设备抓拍失败: " + e.getMessage());
        }
    }
} 